<template>
    <block v-if="scene == 1154">
        <pyq-open-chunk></pyq-open-chunk>
    </block>
    <block v-else>
        <view class="page col-3e">
            <zhihui-wrap :product="productInfo" :detail="priceTableData" v-if="priceTableData"></zhihui-wrap>

			<!-- <bind-seller-bottom-bar :detail="recommendAdviser" v-if="recommendAdviser&&recommendAdviser.avatar_url"></bind-seller-bottom-bar> -->

            <up-gap :height="40" />
            <share-btn :display="scrollAnimate"></share-btn>
        </view>
    </block>
</template>
<script>
    import ZhihuiWrap from './components/zhihui-wrap.vue';
	import { mapState, mapActions } from 'pinia';
    import { useUserInfoStore, useConfigStore, useIndexStore } from '@/store/index';
    import { isBoolean } from '@/common/common';
    import { kaiOneHousePrice } from "@/common/api";
    // import { dispatchClient } from '@/common/api-adviser';
    // import { getSellerInfoById } from '@/common/api/adviser';

	export default {
        components: {
            ZhihuiWrap
        },
		data() {
			return {
                productInfo: null,
                priceTableData: null,
                kaiId: '',
                title: '',
				productId: '',
                share: false,
                start_time: 0,
                scrollAnimate: false,
                recommendAdviser: {}
            }
		},
        computed:{
            ...mapState(useUserInfoStore, ['userInfo']),
            ...mapState(useConfigStore, ['puid', 'scene']),
            ...mapState(useIndexStore, ['latestUserKey']),
        },
        onPageScroll() {
            const that = this;
            that.scrollAnimate = true;
            this.$u.debounce(function() {
                that.scrollAnimate = false;
            }, 500);
        },
        onShareAppMessage() {
            return {
                title: `【${this.title}】智慧选房一房一价表`,
                path: `/pages/product/one-house-one-price/one-house-one-price-table?productId=${this.productId}&kaiId=${this.kaiId}&title=${this.title}&puid=${this.userInfo.id}&share=true&prepage=智慧选房一房一价表-${this.productId}-${this.title}`,
            }
        },
        
        //#ifdef MP-WEIXIN
        onShareTimeline() {
            return {
                title: `【${this.title}】智慧选房一房一价表`,
                query: `productId=${this.productId}&kaiId=${this.kaiId}&title=${this.title}&puid=${this.userInfo.id}&share=true&prepage=智慧选房一房一价表-${this.productId}-${this.title}`
            }
        },
        //#endif
        
		onLoad(options) {
            const that = this;
            
            //朋友圈进入小程序快照场景
            if(this.scene == 1154) return;
            
            let { productId='', kaiId='', puid='', title='', share=false } = options;
            this.productId = productId&&productId!=='undefined'?productId:'';
            this.title = title&&title!=='undefined'?title:'';
            this.share = isBoolean(share);
            this.start_time = (new Date()).getTime();
            if(puid&&puid!=='undefined') {
                this.setPuid(puid)
            }
            this.productInfo = {
                productId: this.productId,
                kaiId: kaiId,
                title: this.title
            }
            //有开盘id先请求一房一价电子表,否则请求一房一价图片
            if(kaiId && kaiId !== "null") {
                this.kaiId = kaiId;
                uni.$check_and_login(function () {
                    that.init()
                })
            }
		},

        methods: {
			...mapActions(useConfigStore, ['setPuid']),

            async init() {
                try {
                    //获取用户最新的加密信息
                    // await getLatestUserManager();

                    //先验证加密需要的session是否过期
                    // await checkSessionThenLogin();

                    //绑定客户
                    // await dispatchClient({
                    //     share: this.share,
                    //     puid: this.puid,
                    //     pid: this.productId
                    // });

                    await this.getOneHousePriceTable();

                    //根据顾问ID获取被绑定的顾问信息
                    // if(this.puid && this.puid !== this.userInfo.id) {
                    //     let sellInfoRes = await getSellerInfoById({id: this.puid, productId: this.productId});
                    //     if(sellInfoRes.status === 0) {
                    //         if(sellInfoRes.detail.product_id && sellInfoRes.detail.product_id == this.productId) {
                    //             this.recommendAdviser = sellInfoRes.detail;
                    //         }
                    //     }
                    // }
                } catch (error) {}
            },
            
            //获取一房一价电子表
            async getOneHousePriceTable(){
                try {
                    const { status, detail } = await kaiOneHousePrice({
                        kaiId: this.kaiId,
                        version: this.latestUserKey && this.latestUserKey.version || ''
                    });
                    if(status !== 0) {
                        uni.$u.toast(detail)
                        return
                    }
                    
                    //解密
                    // let decryptTableRes = await aesCbcDecrypt(detail);
                    this.productInfo.title = this.title = detail.title;
                    this.priceTableData = detail;
                } catch (error) {}
            }
		}
	}
</script>

<style lang="scss" scoped>
	.page{
        line-height: 1;
        min-height: 100vh;
        overflow: hidden;
	}
</style>
